package com.smooth.site.sx.pc.student.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smooth.common.pc.interceptor.PcAccountContext;
import com.smooth.common.site.common.MD5PawService;
import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.rsp.ReturnResult;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.common.site.modules.sys.service.SystemService;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.pc.student.req.StuLoginReq;
import com.smooth.site.sx.pc.student.req.StudenRegVo;
import com.smooth.site.sx.pc.student.service.PcStudentService;
import com.smooth.sys.site.biz.member.gen.service.BizMemberService;
import com.smooth.sys.site.biz.sms.bx.service.SmsCodeService;

@Controller
@RequestMapping(value = "/pc")
public class PcStudentController {
	@Autowired
	BizMemberService bizMemberService;
	@Autowired
	SmsCodeService smsCodeService;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	PcStudentService pcStudentService;

	/**
	 * 学生注册
	 * 
	 * @param studenRegVo
	 * @param result
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "p/stu/register" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult register(@Valid StudenRegVo studenRegVo, BindingResult result, HttpServletRequest request,
			HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudent = pcStudentService.queryByTelNo(studenRegVo.getTelNo());
		if (bizStudent != null) {
			returnResult.setData("message", "手机号" + studenRegVo.getTelNo() + "已经注册，请直接登录");
			returnResult.setData("bizCode", "5002");
		} else {
			bizStudent = pcStudentService.registerStudent(studenRegVo);
			returnResult.setData(bizStudent);
		}
		
		returnResult.setData("stu", bizStudent);
		returnResult.setData("token", MD5PawService.getMd5Psw(bizStudent.getTelNo()));

		PcAccountContext.setObj(bizStudent.getClass(), bizStudent);
		
		return returnResult;
	}

	@InitBinder
	public void intDate(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}

	/**
	 * 学生登录
	 * 
	 * @param stuLoginReq
	 * @param result
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "p/stu/login" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult login(@Valid StuLoginReq stuLoginReq, BindingResult result, HttpServletRequest request,
			HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudent = pcStudentService.queryByTelNo(stuLoginReq.getTelNo());
		if (bizStudent == null) {
			throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户" + stuLoginReq.getTelNo() + "不存在，请注册");
		} else {
			if (StringUtils.isBlank(stuLoginReq.getVerCode())) {
				if (!SystemService.validatePassword(stuLoginReq.getPassword(), bizStudent.getPassword())) {
					throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户" + stuLoginReq.getTelNo() + "密码错误，请重试");
				}
			} else {
				if (!smsCodeService.verfyCode(stuLoginReq.getTelNo(), stuLoginReq.getVerCode())) {
					throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户" + stuLoginReq.getTelNo() + "验证码错误，请重试");
				}
			}
			returnResult.setData("stu", bizStudent);
			returnResult.setData("token", MD5PawService.getMd5Psw(stuLoginReq.getTelNo()));

			PcAccountContext.setObj(bizStudent.getClass(), bizStudent);
		}
		return returnResult;
	}

	/**
	 * 学生补全数据
	 * 
	 * @param stuLoginReq
	 * @param result
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "c/stu/upd" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult upd(@Valid BizStudent bizStudent, BindingResult result,
			HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudentSec = (BizStudent) PcAccountContext.getObj(BizStudent.class);

		BizStudent bizStudentDb = pcStudentService.queryById(bizStudentSec.getId());
		if (bizStudentDb == null) {
			throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户" + bizStudent.getTelNo() + "不存在，请注册");
		} else {
			bizStudent.setId(bizStudentDb.getId());
			returnResult.setData(pcStudentService.updStudent(bizStudent));
		}
		return returnResult;
	}

	@RequestMapping(value = { "c/stu/query" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult query(HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudent = (BizStudent) PcAccountContext.getObj(BizStudent.class);
		BizStudent bizStudentDb = pcStudentService.queryById(bizStudent.getId());
		if (bizStudentDb == null) {
			throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"账户" + bizStudent.getId() + "不存在，请注册");
		} else {
			returnResult.setData(bizStudentDb);
		}
		return returnResult;
	}
}
